package com.ruoyi.train.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.domain.TrainingInstitutionInfomation;

import com.ruoyi.common.mapper.TrainingInstitutionInfomationMapper;

import com.ruoyi.train.domain.*;
import com.ruoyi.train.domain.Vo.CertApplicationStudentVo;
import com.ruoyi.train.domain.Vo.ExamApplicationVo;
import com.ruoyi.train.domain.Vo.ExamPassStudentVo;
import com.ruoyi.train.domain.Vo.StudentInformationVo;
import com.ruoyi.train.mapper.*;
import com.ruoyi.train.service.*;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 学员信息Service业务层处理
 * 
 * @author wusihao
 * @date 2023-03-21
 */
@Service
public class StudentInformationServiceImpl extends ServiceImpl<StudentInformationMapper, StudentInformation> implements IStudentInformationService
{
    @Autowired
    private StudentInformationMapper studentInformationMapper;

    @Autowired
    private TrainClassMapper trainClassMapper;

    @Autowired
    private IStudentInformationService studentInformationService;

    @Autowired
    private StudentProcessRecordMapper studentProcessRecordMapper;
    @Autowired
    private IStudentProcessRecordService studentProcessRecordService;
    @Autowired
    private TrainPlanServiceImpl trainPlanService;


    @Autowired
    ExamApplicationMapper examApplicationMapper;

    @Autowired
    private IExamPassStudentService examPassStudentService;

    @Autowired
    private ICertApplicationStudentService certApplicationStudentService;

    @Autowired
    private TrainingInstitutionInfomationMapper trainingInstitutionInfomationMapper;

    @Autowired
    private ExamApplicationStudentMapper examApplicationStudentMapper;

    @Autowired
    private ISysDictDataService dictDataService;


    /**
     * 查询学员信息
     * 
     * @param id 学员信息主键
     * @return 学员信息
     */
    @Override
    public StudentInformationVo selectStudentInformationById(Long id)
    {
        StudentInformation studentInformation = studentInformationMapper.selectStudentInformationById(id);
        String serialNumber = studentInformation.getSerialNumber();
        QueryWrapper<StudentProcessRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("serial_number", serialNumber);
        StudentProcessRecord studentProcessRecord = studentProcessRecordMapper.selectOne(queryWrapper);
        StudentInformationVo studentInformationVo = new StudentInformationVo();
        studentInformationVo.setTrainConclusion(studentProcessRecord.getTrainConclusion());
        BeanUtils.copyProperties(studentProcessRecord, studentInformationVo);
        BeanUtils.copyProperties(studentInformation, studentInformationVo);

        Long trainPlanId = studentProcessRecord.getTrainPlanId();
        TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
        String qualifiedType = trainPlanServiceById.getQualifiedType();
        String operationType = trainPlanServiceById.getOperationType();
        String workKind = trainPlanServiceById.getWorkKind();

        SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(qualifiedType));
        studentInformationVo.setQualifiedType(dictData.getDictLabel());

        if (operationType != null && !operationType.equals("")) {
            SysDictData dictData1 = dictDataService.selectDictDataById(Long.parseLong(operationType));
            studentInformationVo.setOperationType(dictData1.getDictLabel());
        }


        SysDictData dictData2 = dictDataService.selectDictDataById(Long.parseLong(workKind));
        studentInformationVo.setWorkKind(dictData.getDictLabel() + dictData2.getDictLabel());
        return studentInformationVo;
    }

    /**
     * 查询学员信息列表
     * 
     * @param studentInformation 学员信息
     * @return 学员信息
     */
    @Override
    public List<StudentInformation> selectStudentInformationList(StudentInformation studentInformation)
    {
        List<StudentInformation> studentInformations = studentInformationMapper.selectStudentInformationList(studentInformation);
        List<StudentInformation> ans = new ArrayList<>();
        for (StudentInformation studentInformation1 : studentInformations)
                if (studentInformation1.getIsDeleted() == 0)
                        ans.add(studentInformation1);
        return ans;
    }

    /**
     * 新增学员信息
     * 
     * @param studentInformation 学员信息
     * @return 结果
     */
    @Override
    public int insertStudentInformation(StudentInformation studentInformation)
    {
        studentInformation.setCreateTime(DateUtils.getNowDate());
        // 通过培训班级id找到培训班级信息，得到培训期次
        Long classId = studentInformation.getClassId();
        TrainClass trainClass = trainClassMapper.selectTrainClassById(classId);
        String classesTerm = trainClass.getClassesTerm();

        // 通过培训班级表找到培训计划表，将数据导入学生三项流程岗位
        Long trainPlanId = trainClass.getTrainPlanId();
        TrainPlan trainPlan = trainPlanService.selectTrainPlanById(trainPlanId);
        studentInformation.setCreateId(SecurityUtils.getUserId());

        String qualifiedType = trainPlan.getQualifiedType();
        if (qualifiedType != null && qualifiedType.length() > 0) {
            SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(qualifiedType));
            String dictLabel = dictData.getDictLabel();
            studentInformation.setQualifiedType(dictLabel);
        }

        boolean save = studentInformationService.save(studentInformation);
        if (save) {
            // 设置学生序号为班级期次 + 班级id +  "-" + 学生id
            Long id = studentInformation.getId();
            studentInformation.setSerialNumber(classesTerm + "" + classId +"-" +  id);
            studentInformationService.updateStudentInformation(studentInformation);

            // 添加到学员三项岗位航程记录表
            StudentProcessRecord studentProcessRecord = new StudentProcessRecord();
            studentProcessRecord.setClassId(studentInformation.getClassId());
            studentProcessRecord.setIsDeleted(studentInformation.getIsDeleted());
            studentProcessRecord.setSerialNumber(studentInformation.getSerialNumber());
            studentProcessRecord.setExamPlace(trainPlan.getExamPlace());
            studentProcessRecord.setTrainPlanId(trainPlan.getId());
            return studentProcessRecordMapper.insertStudentProcessRecord(studentProcessRecord);
        }
        return 0;
    }

    /**
     * 修改学员信息
     * 
     * @param studentInformation 学员信息
     * @return 结果
     */
    @Override
    public int updateStudentInformation(StudentInformation studentInformation)
    {
        studentInformation.setUpdateId(SecurityUtils.getUserId());
        studentInformation.setUpdateTime(DateUtils.getNowDate());
        return studentInformationMapper.updateStudentInformation(studentInformation);
    }

    /**
     * 批量删除学员信息
     * 
     * @param ids 需要删除的学员信息主键
     * @return 结果
     */
    @Override
    public int deleteStudentInformationByIds(Long[] ids)
    {
        for (int i = 0; i < ids.length; i ++ ) {
            StudentInformation studentInformation = studentInformationMapper.selectById(ids[i]);
            studentInformation.setUpdateId(SecurityUtils.getUserId());
            String serialNumber = studentInformation.getSerialNumber();
            QueryWrapper<StudentProcessRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("serial_number", serialNumber);
            StudentProcessRecord studentProcessRecord = studentProcessRecordMapper.selectOne(queryWrapper);
            studentProcessRecord.setIsDeleted(1L);
            studentProcessRecordMapper.updateStudentProcessRecord(studentProcessRecord);
        }
        return studentInformationMapper.deleteStudentInformationByIds(ids);
    }

    @Override
    public List<StudentInformation> selectStudentByClassId(Long classId) {
        QueryWrapper<StudentInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", classId);
        queryWrapper.eq("is_deleted", 0);
        List<StudentInformation> studentInformations = studentInformationMapper.selectList(queryWrapper);
        return studentInformations;
    }

    /*
    * 提交班级附件信息
    *
    * */
    @Override
    public int submitClassInfo(TrainClass classInfo) {
        return trainClassMapper.updateTrainClass(classInfo);
    }

    @Override
    public List<StudentInformationVo> selectStudentVoByApplicationId(Long examApplicationId) {
        List<StudentInformationVo> studentInformationVos = new ArrayList<>();
         QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
         // 通过考试申请表id查询考生id
         examApplicationStudentQueryWrapper.eq("exam_application_id", examApplicationId);
        List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentMapper.selectList(examApplicationStudentQueryWrapper);
        for (ExamApplicationStudent examApplicationStudent : examApplicationStudents) {
            StudentInformationVo studentInformationVo = new StudentInformationVo();
            // 得到学生id
            Long studentId = examApplicationStudent.getStudentId();
            // 通过学生id查询学生信息表
            QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
            studentInformationQueryWrapper.eq("id", studentId);
            StudentInformation studentInformation = studentInformationMapper.selectOne(studentInformationQueryWrapper);
            // 通过学生编号查询学生记录表信息
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("serial_number", studentInformation.getSerialNumber());
            StudentProcessRecord studentProcessRecord = studentProcessRecordMapper.selectOne(studentProcessRecordQueryWrapper);

            BeanUtils.copyProperties(studentProcessRecord, studentInformationVo);
            BeanUtils.copyProperties(studentInformation, studentInformationVo);

            Long trainPlanId = studentProcessRecord.getTrainPlanId();
            TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
            String operationType = trainPlanServiceById.getOperationType();
            if (operationType != null && !operationType.equals("")) {
                SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(operationType));
                studentInformationVo.setOperationType(dictData.getDictLabel());
            }

            String qualifiedType = trainPlanServiceById.getQualifiedType();
            SysDictData dictData1 = dictDataService.selectDictDataById(Long.parseLong(qualifiedType));
            studentInformationVo.setQualifiedType(dictData1.getDictLabel());
            studentInformationVos.add(studentInformationVo);
        }
        return studentInformationVos;
    }

    @Override
    public String importData(List<StudentInformationVo> studentInformationVoList, String operName)
    {
        if (StringUtils.isNull(studentInformationVoList) || studentInformationVoList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (StudentInformationVo studentInformationVo : studentInformationVoList)
        {
            try
            {
                // 验证是否存在这个用户
                QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
                studentInformationQueryWrapper.eq("id_card", studentInformationVo.getIdCard());
                studentInformationQueryWrapper.eq("name", studentInformationVo.getName());
                StudentInformation studentInformation = studentInformationMapper.selectOne(studentInformationQueryWrapper);
                // 找到学生记录表
                QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
                studentProcessRecordQueryWrapper.eq("serial_number", studentInformation.getSerialNumber());

                StudentProcessRecord studentProcessRecord = studentProcessRecordMapper.selectOne(studentProcessRecordQueryWrapper);
                studentProcessRecord.setExamScore(studentInformationVo.getExamScore());
                studentProcessRecordMapper.updateById(studentProcessRecord);


                if (!StringUtils.isNull(studentProcessRecord))
                {
                    studentProcessRecordMapper.updateById(studentProcessRecord);
                    successNum++;
                    successMsg.append("<br/>" + studentInformationVo.getName() +  " 导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + studentInformationVo.getName() + " 不存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + studentInformationVo.getName() +  " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    // 添加实操考试结论
    @Override
    public int addoperatingScore(StudentInformationVo studentInformationVo) {
        Long id = studentInformationVo.getId();
        StudentInformation studentInformation = studentInformationMapper.selectById(id);
        String serialNumber = studentInformation.getSerialNumber();
        QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
        studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
        StudentProcessRecord studentProcessRecord = studentProcessRecordMapper.selectOne(studentProcessRecordQueryWrapper);
        studentProcessRecord.setOperatingScore(studentInformationVo.getOperatingScore());
        return studentProcessRecordMapper.updateStudentProcessRecord(studentProcessRecord);
    }

    /*
     * 通过考试合格证明表id得到学生信息
     * */
    @Override
    public List<ExamPassStudentVo> selectStudentInformationByExamPassCertificateId(Long examPassCertificateId) {
        // 最终返回的答案集合
        List<ExamPassStudentVo> examPassStudentVos = new ArrayList<>();
        // 通过考试合格证明表id查询考试合格证明表学生信息
        QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
        examPassStudentQueryWrapper.eq("exam_pass_id", examPassCertificateId);
        List<ExamPassStudent> examPassStudentList = examPassStudentService.list(examPassStudentQueryWrapper);
        for (int i = 0; i < examPassStudentList.size(); i ++ ) {
            ExamPassStudentVo examPassStudentVo = new ExamPassStudentVo();
            // 通过考试合格证明学生表得到学生id
            ExamPassStudent examPassStudent = examPassStudentList.get(i);
            Long studentId = examPassStudent.getStudentId();
            // 通过学生id得到学生信息表，学生记录表
            QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
            studentInformationQueryWrapper.eq("id", studentId);
            studentInformationQueryWrapper.eq("is_deleted", 0);
            StudentInformation studentInformation = studentInformationService.getOne(studentInformationQueryWrapper);
            // 将学生信息表的数据加入到答案中
            examPassStudentVo.setName(studentInformation.getName());
            examPassStudentVo.setPhone(studentInformation.getPhone());
            examPassStudentVo.setIdCard(studentInformation.getIdCard());
            examPassStudentVo.setWorkplace(studentInformation.getWorkplace());
            examPassStudentVo.setId(studentInformation.getId());
            examPassStudentVo.setUrlPersonApplication(studentInformation.getUrlPersonApplication());
            examPassStudentVo.setUrlHealthCertificate(studentInformation.getUrlHealthCertificate());
            examPassStudentVo.setUrlDegreeCertificate(studentInformation.getUrlDegreeCertificate());
            examPassStudentVo.setUrlIdCopy(studentInformation.getUrlIdCopy());
            // 通过学生信息表中的学生编号得到学生记录表
            String serialNumber = studentInformation.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
            studentProcessRecordQueryWrapper.eq("exam_application_is_pass", 1);
            studentProcessRecordQueryWrapper.eq("exam_certificate_is_pass", 1);
            StudentProcessRecord studentProcessRecord = studentProcessRecordMapper.selectOne(studentProcessRecordQueryWrapper);
            if (studentProcessRecord == null) continue;
            Integer examCertificateIsPass = studentProcessRecord.getExamCertificateIsPass();
            if (examCertificateIsPass == 1) {
                examPassStudentVo.setExamConclusion("合格");
            } else {
                examPassStudentVo.setExamConclusion("不合格");
            }
            if(studentProcessRecord == null) continue;
            Long trainPlanId = studentProcessRecord.getTrainPlanId();
            TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
            examPassStudentVo.setWorkKind(trainPlanServiceById.getWorkKind());
            examPassStudentVo.setQualifiedType(trainPlanServiceById.getQualifiedType());
            examPassStudentVo.setOperationType(trainPlanServiceById.getOperationType());
            examPassStudentVo.setExamScore(studentProcessRecord.getExamScore());
            examPassStudentVo.setOperatingScore(studentProcessRecord.getOperatingScore());
            examPassStudentVo.setExamIsPass(studentProcessRecord.getExamIsPass());

            examPassStudentVo.setExamCertificateIsPass(studentProcessRecord.getExamCertificateIsPass());

            // 将答案加入到最终的集合中
            examPassStudentVos.add(examPassStudentVo);
        }
        return examPassStudentVos;
    }

    // 是否确实通过培训
    @Override
    public int confirmTrainisPassAll(Long classId) {
        // 得到当前用户登录id以便自动录入信息
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<TrainingInstitutionInfomation> trainingInstitutionInfomationQueryWrapper = new QueryWrapper<>();
        trainingInstitutionInfomationQueryWrapper.eq("create_id", SecurityUtils.getUserId());
        TrainingInstitutionInfomation trainingInstitutionInfomation = trainingInstitutionInfomationMapper.selectOne(trainingInstitutionInfomationQueryWrapper);
        String applicationUnitSheet = trainingInstitutionInfomation.getApplicationUnitSheet();
        String urlFeeCode = trainingInstitutionInfomation.getUrlFeeCode();

        // 通过培训
        TrainClass trainClass = trainClassMapper.selectById(classId);
        trainClass.setIsEnd(1);
        trainClass.setUrlFeeCode(urlFeeCode);

        // 通过班级id查找学员信息
        QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
        studentProcessRecordQueryWrapper.eq("class_id", classId);
        studentProcessRecordQueryWrapper.eq("is_deleted", 0);
        List<StudentProcessRecord> studentProcessRecords = studentProcessRecordMapper.selectList(studentProcessRecordQueryWrapper);

        // 将学员记录表设置为已经培训合格
        for (StudentProcessRecord studentProcessRecord : studentProcessRecords) {
            studentProcessRecord.setTrainIsPass(1);
            studentProcessRecordMapper.updateById(studentProcessRecord);
        }

        List<StudentInformation> studentInformations = new ArrayList<>();
        // 通过学生记录表中的学生编号得到学生集合
        for (StudentProcessRecord studentProcessRecord : studentProcessRecords) {
            String serialNumber = studentProcessRecord.getSerialNumber();
            QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
            studentInformationQueryWrapper.eq("serial_number", serialNumber);
            StudentInformation studentInformation = studentInformationMapper.selectOne(studentInformationQueryWrapper);
            if (studentInformation == null) continue;
            if (studentInformation.getIsDeleted() == 1) continue;
            studentInformations.add(studentInformation);
        }
        // 将信息放入考试申请表中
        ExamApplication examApplication = new ExamApplication();
        examApplication.setCreateId(userId);
        examApplication.setSum(studentProcessRecords.size());
        examApplication.setExamKind(studentProcessRecords.get(0).getExamType());
        examApplication.setApplicationUnitSheet(applicationUnitSheet);
        examApplication.setClassTerm(trainClass.getClassesTerm());
        examApplication.setSendTime(DateUtils.getNowDate());
        examApplication.setCreateTime(DateUtils.getNowDate());
        examApplication.setExamPlace(studentProcessRecords.get(0).getExamPlace());
        Integer is_true =  examApplicationMapper.insert(examApplication);
        Long applicationId = examApplication.getId();

        ExamApplicationVo examApplicationVo = new ExamApplicationVo();
        BeanUtils.copyProperties(examApplication, examApplicationVo);
        // 将学生信息表中的信息读入考试申请表
        List<ExamApplicationStudent> examApplicationStudents = new ArrayList<>();
        for (StudentInformation studentInformation : studentInformations) {
            Long id = studentInformation.getId();
            ExamApplicationStudent examApplicationStudent = new ExamApplicationStudent();
            examApplicationStudent.setStudentId(id);
            examApplicationStudent.setExamApplicationId(applicationId);
            examApplicationStudent.setStudentId(id);
            examApplicationStudents.add(examApplicationStudent);
        }

        for (ExamApplicationStudent examApplicationStudent : examApplicationStudents) {
            examApplicationStudentMapper.insert(examApplicationStudent);
        }

        return trainClassMapper.updateTrainClass(trainClass);
    }

    @Override
    public String importStudentInformation(List<StudentInformation> studentInformationList, String operName, Long classId, Long trainId) {
        if (StringUtils.isNull(studentInformationList) || studentInformationList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (StudentInformation studentInformation : studentInformationList)
        {
            try
            {
                QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
                studentInformationQueryWrapper.eq("class_id", classId);
                studentInformationQueryWrapper.eq("name", studentInformation.getName());
                studentInformationQueryWrapper.eq("id_card", studentInformation.getIdCard());
                studentInformationQueryWrapper.eq("workplace", studentInformation.getWorkplace());
                studentInformationQueryWrapper.eq("phone", studentInformation.getPhone());
                StudentInformation studentInformationServiceOne = studentInformationService.getOne(studentInformationQueryWrapper);

                if (StringUtils.isNull(studentInformationServiceOne))
                {
                    // 通过班级id查询班级期次
                    TrainClass trainClass = trainClassMapper.selectById(classId);
                    Long trainPlanId = trainClass.getTrainPlanId();
                    TrainPlan trainPlanServiceById1 = trainPlanService.getById(trainPlanId);
                    String qualifiedType = trainPlanServiceById1.getQualifiedType();



                    StudentInformation student = new StudentInformation();
                    if (qualifiedType != null && qualifiedType.length() > 0) {
                        SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(qualifiedType));
                        String dictLabel = dictData.getDictLabel();
                        student.setQualifiedType(dictLabel);
                    }
                    student.setName(studentInformation.getName());
                    student.setIdCard(studentInformation.getIdCard());
                    student.setWorkplace(studentInformation.getWorkplace());
                    student.setPhone(studentInformation.getPhone());
                    student.setClassId(classId);
                    boolean save = studentInformationService.save(student);
                    student.setSerialNumber(trainClass.getClassesTerm() + "" + classId + "-" + student.getId());
                    studentInformationService.updateById(student);

                    StudentProcessRecord studentProcessRecord = new StudentProcessRecord();
                    // 通过培训计划id得到操作项目和考试地点
                    TrainPlan trainPlanServiceById = trainPlanService.getById(trainId);
                    studentProcessRecord.setExamPlace(trainPlanServiceById.getExamPlace());
                    studentProcessRecord.setSerialNumber(trainClass.getClassesTerm() + "" + classId + "-" + student.getId());
                    studentProcessRecord.setTrainPlanId(trainId);
                    studentProcessRecord.setClassId(classId);
                    studentProcessRecordService.save(studentProcessRecord);

                    if (save) {
                        successNum++;
                        successMsg.append("<br/>" + studentInformation.getName() +  " 导入成功");
                    } else {
                        failureNum++;
                        failureMsg.append("<br/>" + studentInformation.getName() + " 添加失败");
                    }
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + studentInformation.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + studentInformation.getName() +  " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    // 选择学生是否通过考试合格证明表
    @Override
    public int selectStudentExamCertificate(Long[] ids) {
        for (int i = 0; i < ids.length; i ++ ) {
            StudentInformation selectById = studentInformationMapper.selectById(ids[i]);
            String serialNumber = selectById.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
            StudentProcessRecord studentProcessRecordServiceOne = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
            studentProcessRecordServiceOne.setExamCertificateIsPass(1);
            studentProcessRecordService.updateById(studentProcessRecordServiceOne);
        }
        return 1;
    }

    // 通过考试合格证明表查到所有学生信息(peixunjigou1)
    @Override
    public List<?> selectAllStudentInformationByExamPassCertificateId(Long examPassCertificateId) {
        // 最终返回的答案集合
        List<ExamPassStudentVo> examPassStudentVos = new ArrayList<>();
        // 通过考试合格证明表id查询考试合格证明表学生信息
        QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
        examPassStudentQueryWrapper.eq("exam_pass_id", examPassCertificateId);
        List<ExamPassStudent> examPassStudentList = examPassStudentService.list(examPassStudentQueryWrapper);
        for (int i = 0; i < examPassStudentList.size(); i ++ ) {
            ExamPassStudentVo examPassStudentVo = new ExamPassStudentVo();
            // 通过考试合格证明学生表得到学生id
            ExamPassStudent examPassStudent = examPassStudentList.get(i);
            Long studentId = examPassStudent.getStudentId();
            // 通过学生id得到学生信息表，学生记录表
            StudentInformation studentInformation = studentInformationService.getById(studentId);
            if (studentInformation.getIsDeleted() == 1) continue;
            // 将学生信息表的数据加入到答案中
            examPassStudentVo.setName(studentInformation.getName());
            examPassStudentVo.setPhone(studentInformation.getPhone());
            examPassStudentVo.setIdCard(studentInformation.getIdCard());
            examPassStudentVo.setWorkplace(studentInformation.getWorkplace());
            examPassStudentVo.setId(studentInformation.getId());
            examPassStudentVo.setUrlPersonApplication(studentInformation.getUrlPersonApplication());
            examPassStudentVo.setUrlHealthCertificate(studentInformation.getUrlHealthCertificate());
            examPassStudentVo.setUrlDegreeCertificate(studentInformation.getUrlDegreeCertificate());
            examPassStudentVo.setUrlIdCopy(studentInformation.getUrlIdCopy());
            // 通过学生信息表中的学生编号得到学生记录表
            String serialNumber = studentInformation.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
            studentProcessRecordQueryWrapper.eq("exam_application_is_pass", 1);
            StudentProcessRecord studentProcessRecord = studentProcessRecordMapper.selectOne(studentProcessRecordQueryWrapper);
            if(studentProcessRecord == null) continue;
            Long trainPlanId = studentProcessRecord.getTrainPlanId();
            TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
            examPassStudentVo.setOperationType(trainPlanServiceById.getOperationType());
            examPassStudentVo.setExamScore(studentProcessRecord.getExamScore());
            examPassStudentVo.setOperatingScore(studentProcessRecord.getOperatingScore());
            examPassStudentVo.setExamIsPass(studentProcessRecord.getExamIsPass());
            examPassStudentVo.setExamCertificateIsPass(studentProcessRecord.getExamCertificateIsPass());

            // 将答案加入到最终的集合中
            examPassStudentVos.add(examPassStudentVo);
        }
        return examPassStudentVos;
    }

    @Override
    public int selectStudentSubmitCert(Long[] ids) {
        for (int i = 0; i < ids.length; i ++ ) {
            StudentInformation selectById = studentInformationMapper.selectById(ids[i]);
            String serialNumber = selectById.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
            StudentProcessRecord studentProcessRecordServiceOne = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
            studentProcessRecordServiceOne.setCertIsPass(1);
            studentProcessRecordService.updateById(studentProcessRecordServiceOne);
        }
        return 1;
    }

    @Override
    public List<CertApplicationStudentVo> selectAllStudentInformationByCertApplicationId(Long certApplicationId) {
        // 存取答案集合
        List<CertApplicationStudentVo> certApplicationStudentVos = new ArrayList<>();
        // 通过办证申请表id查询办证申请学员表得到学生id
        QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
        certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationId);
        List<CertApplicationStudent> certApplicationStudents = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
        for (CertApplicationStudent certApplicationStudent : certApplicationStudents) {
            // 定义答案实例
            CertApplicationStudentVo certApplicationStudentVo = new CertApplicationStudentVo();
            // 得到学生id
            Long studentId = certApplicationStudent.getStudentId();
            // 通过学生id查询学生基本信息表
            QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
            studentInformationQueryWrapper.eq("id", studentId);
            studentInformationQueryWrapper.eq("is_deleted", 0);
            StudentInformation studentInformation = studentInformationService.getOne(studentInformationQueryWrapper);
            // 将学生信息加入答案实例
            certApplicationStudentVo.setId(studentInformation.getId());
            certApplicationStudentVo.setIdCard(studentInformation.getIdCard());
            certApplicationStudentVo.setName(studentInformation.getName());
            certApplicationStudentVo.setWorkplace(studentInformation.getWorkplace());
            certApplicationStudentVo.setPhone(studentInformation.getPhone());
            certApplicationStudentVo.setUrlPersonApplication(studentInformation.getUrlPersonApplication());
            certApplicationStudentVo.setUrlHealthCertificate(studentInformation.getUrlHealthCertificate());
            certApplicationStudentVo.setUrlDegreeCertificate(studentInformation.getUrlDegreeCertificate());
            certApplicationStudentVo.setUrlIdCopy(studentInformation.getUrlIdCopy());

            // 通过学生序号查询学生记录表
            String serialNumber = studentInformation.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
            studentProcessRecordQueryWrapper.eq("exam_application_is_pass", 1);
            studentProcessRecordQueryWrapper.eq("exam_certificate_is_pass", 1);
            StudentProcessRecord studentProcessRecord = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
            if (studentProcessRecord == null) continue;
            // 判断学生类型是特作还是安全管理人员
            certApplicationStudentVo.setExamScore(studentProcessRecord.getExamScore());
            Long trainPlanId = studentProcessRecord.getTrainPlanId();
            TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
            certApplicationStudentVo.setOperationType(trainPlanServiceById.getOperationType());
            certApplicationStudentVo.setCertIsPass(studentProcessRecord.getCertIsPass());

            // 加入答案集合
            certApplicationStudentVos.add(certApplicationStudentVo);
        }
        return certApplicationStudentVos;
    }

    /*
     * 通过办证申请表id得到学生信息
     * */
    @Override
    public List<CertApplicationStudentVo>  selectStudentInformationByCertApplicationId(Long certApplicationId) {
        // 存取答案集合
        List<CertApplicationStudentVo> certApplicationStudentVos = new ArrayList<>();
        // 通过办证申请表id查询办证申请学员表得到学生id
        QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
        certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationId);
        List<CertApplicationStudent> certApplicationStudents = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
        for (CertApplicationStudent certApplicationStudent : certApplicationStudents) {
            // 定义答案实例
            CertApplicationStudentVo certApplicationStudentVo = new CertApplicationStudentVo();
            // 得到学生id
            Long studentId = certApplicationStudent.getStudentId();
            // 通过学生id查询学生基本信息表
            QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
            studentInformationQueryWrapper.eq("id", studentId);
            studentInformationQueryWrapper.eq("is_deleted", 0);
            StudentInformation studentInformation = studentInformationService.getOne(studentInformationQueryWrapper);
            if (studentInformation == null) continue;
            // 将学生信息加入答案实例
            certApplicationStudentVo.setId(studentInformation.getId());
            certApplicationStudentVo.setIdCard(studentInformation.getIdCard());
            certApplicationStudentVo.setName(studentInformation.getName());
            certApplicationStudentVo.setWorkplace(studentInformation.getWorkplace());
            certApplicationStudentVo.setPhone(studentInformation.getPhone());
            certApplicationStudentVo.setUrlPersonApplication(studentInformation.getUrlPersonApplication());
            certApplicationStudentVo.setUrlHealthCertificate(studentInformation.getUrlHealthCertificate());
            certApplicationStudentVo.setUrlDegreeCertificate(studentInformation.getUrlDegreeCertificate());
            certApplicationStudentVo.setUrlIdCopy(studentInformation.getUrlIdCopy());

            // 通过学生序号查询学生记录表
            String serialNumber = studentInformation.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
            studentProcessRecordQueryWrapper.eq("exam_application_is_pass", 1);
            studentProcessRecordQueryWrapper.eq("exam_certificate_is_pass", 1);
            studentProcessRecordQueryWrapper.eq("cert_is_pass", 1);
            StudentProcessRecord studentProcessRecord = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
            if (studentProcessRecord == null) continue;
            // 判断学生类型是特作还是安全管理人员
            certApplicationStudentVo.setExamScore(studentProcessRecord.getExamScore());
            Long trainPlanId = studentProcessRecord.getTrainPlanId();
            TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
            certApplicationStudentVo.setOperationType(trainPlanServiceById.getOperationType());
            certApplicationStudentVo.setCertIsPass(studentProcessRecord.getCertIsPass());

            // 加入答案集合
            certApplicationStudentVos.add(certApplicationStudentVo);
        }
        return certApplicationStudentVos;
    }

}
